<template>
	<div class="page-wrap">
		<!-- 气象设置 -->
		<div class="page-content p-0">
			<div class="page-column p-0">
				<div class="page-form p-x-0">
					<el-form
						:model="ruleForm"
						:rules="rules"
						label-position="left"
						label-width="160px"
						ref="form"
						label-suffix="："
					>
						<el-row>
							<el-col :span="24">
								<div class="my_title m-b-20">气象文件类型</div>
								<el-form-item label="气象文件类型" prop="M1METCOD001">
									<select-para
										style="width:100%"
										size="medium"
										class="mediumForm"
										v-model="ruleForm.M1METCOD001"
										:label.sync="ruleForm.M1METCOD001Name"
										@change="M1METCOD001Change"
										:options="M1METCOD001options"
									></select-para>
								</el-form-item>
								<el-form-item label="用户自定义气象文件" v-if="ruleForm.M1METCOD001=='3'" prop>
									<el-upload
										class="upload-demo"
										action="https://jsonplaceholder.typicode.com/posts/"
										:on-preview="handlePreview"
										:on-remove="handleRemove"
										:before-remove="beforeRemove"
										multiple
										:limit="3"
										:on-exceed="handleExceed"
										:file-list="fileList"
									>
										<el-button size="small" type="primary">上传文件</el-button>
									</el-upload>
								</el-form-item>
								<el-form-item label="用户自定义气象文件" v-if="ruleForm.M1METCOD001=='5'" prop>
									<el-upload
										class="upload-demo"
										action='javascript:;'
										:http-request='uploadWeaterFile'	
										:before-upload='beforeUpload'
										:show-file-list='false'
									>
										<el-button size="small" type="primary">上传文件</el-button>
									</el-upload>
								</el-form-item>
							</el-col>
						</el-row>
						<div class="separate m-b-20"></div>
						<el-row>
							<el-col :span="24">
								<div class="my_title m-b-20">气象参数设置</div>
								<el-form-item label="气象数据使用的最大距离" prop="M2LIMSPA001">
									<select-para
										size="medium"
										class="mediumForm"
										v-model="ruleForm.M2LIMSPA001"
										:label.sync="ruleForm.M2LIMSPA001Name"
										:options="M2LIMSPA001options"
									></select-para>
								</el-form-item>
								<el-form-item label="边界气象混合层高度" prop="M2BNDMXH001">
									<el-input size="medium" class="mediumForm" v-model="ruleForm.M2BNDMXH001">
										<template slot="append">m</template>
									</el-input>
								</el-form-item>
								<el-form-item label="边界气象稳定度" prop="M2IBDSTB001">
									<el-input size="medium" class="mediumForm" v-model.number="ruleForm.M2IBDSTB001"></el-input>
								</el-form-item>
								<el-form-item label="边界气象降雨量" prop="M2BNDRAN001">
									<el-input size="medium" class="mediumForm" v-model="ruleForm.M2BNDRAN001">
										<template slot="append">mm/h</template>
									</el-input>
								</el-form-item>
								<el-form-item label="边界气象风速" prop="M2BNDWND001">
									<el-input size="medium" class="mediumForm" v-model="ruleForm.M2BNDWND001">
										<template slot="append">m/s</template>
									</el-input>
								</el-form-item>
								<el-form-item
									label="气象抽样开始时间"
									prop="M3ISTRDY001"
									v-if="ruleForm.M1METCOD001 == 1 || ruleForm.M1METCOD001 == 3 || ruleForm.M1METCOD001 == 4"
								>
									<el-input size="medium" class="mediumForm" v-model.number="ruleForm.M3ISTRDY001">
										<template slot="append">day</template>
									</el-input>
								</el-form-item>
								<el-form-item
									label="气象抽样开始时间"
									prop="M3ISTRHR001"
									v-if="ruleForm.M1METCOD001 == 1 || ruleForm.M1METCOD001 == 3 || ruleForm.M1METCOD001 == 4"
								>
									<el-input size="medium" class="mediumForm" v-model.number="ruleForm.M3ISTRHR001">
										<template slot="append">hour</template>
									</el-input>
								</el-form-item>
								<el-form-item label="降雨分隔数量" prop="M4NRNINT001" v-if="ruleForm.M1METCOD001 == 2 ">
									<el-input size="medium" class="mediumForm" v-model.number="ruleForm.M4NRNINT001"></el-input>
								</el-form-item>
								<el-form-item label="降雨分隔距离" prop="M4RNDSTS001" v-if="ruleForm.M1METCOD001 == 2 ">
									<el-input size="medium" class="mediumForm" v-model="ruleForm.M4RNDSTS001">
										<template slot="append">m</template>
									</el-input>
								</el-form-item>
								<el-form-item label="降雨强度分隔数量" prop="M4NRINTN001" v-if="ruleForm.M1METCOD001 == 2 ">
									<el-input size="medium" class="mediumForm" v-model.number="ruleForm.M4NRINTN001"></el-input>
								</el-form-item>
								<el-form-item label="降雨分隔强度" prop="M4RNRATE001" v-if="ruleForm.M1METCOD001 == 2 ">
									<el-input size="medium" class="mediumForm" v-model="ruleForm.M4RNRATE001"></el-input>
								</el-form-item>
								<el-form-item label="bin文件样品数" prop="M4NSMPLS001">
									<select-para
										size="medium"
										class="mediumForm"
										v-model="ruleForm.M4NSMPLS001"
										:label.sync="ruleForm.M4NSMPLS001"
										@change="M4NSMPLS001Change"
										:options="M4NSMPLS001options"
									></select-para>
								</el-form-item>
								<el-form-item label="随机生成起始数" prop="M4IRSEED001">
									<el-input size="medium" class="mediumForm" v-model="ruleForm.M4IRSEED001"></el-input>
								</el-form-item>
								<el-form-item label="气象类别数目" prop="M4NSBINS001">
									<div>{{ruleForm.M4NSBINS001}}</div>
								</el-form-item>

								<div>
									<div class="operation">
										<el-button type="primary" size="small" @click="exportExcel">
											<img :style="{width: '12px',height: '12px'}" src="/img/basic/daochu.png" alt />
											&nbsp;导出
										</el-button>
										<el-button type="primary" size="small" @click="daoru(1)">
											<img :style="{width: '12px',height: '12px'}" src="/img/basic/daoru.png" alt />
											&nbsp;导入
										</el-button>
										<input
											ref="file"
											id="img-upload"
											class="selectFile"
											type="file"
											@change="importf(this)"
											accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
										/>
									</div>
									<el-table ref="multipleTable" :data="ruleForm.M4SMPLDFtab" tooltip-effect="dark" border>
										<el-table-column prop="index" label="气象类别编号" min-width="120" align="center"></el-table-column>
										<el-table-column prop="M4SMPLDF_INDXBN" label="气象bin编号" align="center" min-width="120">
											<template slot-scope="scope">
												<el-form-item
													label-width="0px"
													:rules="rules.M4SMPLDF_INDXBN"
													:prop="'M4SMPLDFtab.'+scope.$index+'.M4SMPLDF_INDXBN'"
												>
													<el-input
														oninput="value=value.replace(/^\D*(\d*(?:\.\d{0,20})?).*$/g, '$1')"
														class="bigForm"
														v-model="scope.row.M4SMPLDF_INDXBN"
													></el-input>
												</el-form-item>
											</template>
										</el-table-column>
										<el-table-column prop="M4SMPLDF_INWGHT" label="气象序列数量" min-width="120" align="center">
											<template slot-scope="scope">
												<el-form-item
													label-width="0px"
													:rules="rules.M4SMPLDF_INWGHT"
													:prop="'M4SMPLDFtab.'+scope.$index+'.M4SMPLDF_INWGHT'"
												>
													<el-input
														oninput="value=value.replace(/^\D*(\d*(?:\.\d{0,20})?).*$/g, '$1')"
														class="bigForm"
														v-model="scope.row.M4SMPLDF_INWGHT"
													></el-input>
												</el-form-item>
											</template>
										</el-table-column>
									</el-table>
								</div>
								<div v-if="this.ruleForm.M4NSMPLS001=='3'">
									<div class="operation">
										<el-button type="primary" size="small" @click="daochu()">
											<img :style="{width: '12px',height: '12px'}" src="/img/basic/daochu.png" alt />
											&nbsp;导出
										</el-button>
										<el-button type="primary" size="small" @click="daoru(2)">
											<img :style="{width: '12px',height: '12px'}" src="/img/basic/daoru.png" alt />
											&nbsp;导入
										</el-button>
										<input
											ref="file2"
											id="img-upload"
											class="selectFile"
											type="file"
											@change="importf(this)"
											accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
										/>
									</div>
									<el-table ref="multipleTable" :data="ruleForm.M5METDATtab" tooltip-effect="dark" border>
										<el-table-column prop="M5METDAT_HRMXHT" label="混合层高度（m）" align="center" min-width="120">
											<template slot-scope="scope">
												<el-form-item
													label-width="0px"
													:rules="rules.M5METDAT_HRMXHT"
													:prop="'M5METDATtab.'+scope.$index+'.M5METDAT_HRMXHT'"
												>
													<el-input
														oninput="value=value.replace(/^\D*(\d*(?:\.\d{0,20})?).*$/g, '$1')"
														class="bigForm"
														v-model="scope.row.M5METDAT_HRMXHT"
													></el-input>
												</el-form-item>
											</template>
										</el-table-column>
										<el-table-column prop="M5METDAT_IHRSTB" label="稳定度类型" align="center" min-width="120">
											<template slot-scope="scope">
												<el-form-item
													label-width="0px"
													:rules="rules.M5METDAT_IHRSTB"
													:prop="'M5METDATtab.'+scope.$index+'.M5METDAT_IHRSTB'"
												>
													<el-input
														oninput="value=value.replace(/^\D*(\d*(?:\.\d{0,20})?).*$/g, '$1')"
														class="bigForm"
														v-model="scope.row.M5METDAT_IHRSTB"
													></el-input>
												</el-form-item>
											</template>
										</el-table-column>
										<el-table-column prop="M5METDAT_HRRAIN" label="降雨速度（mm/h）" min-width="120" align="center">
											<template slot-scope="scope">
												<el-form-item
													label-width="0px"
													:rules="rules.M5METDAT_HRRAIN"
													:prop="'M5METDATtab.'+scope.$index+'.M5METDAT_HRRAIN'"
												>
													<el-input
														oninput="value=value.replace(/^\D*(\d*(?:\.\d{0,20})?).*$/g, '$1')"
														class="bigForm"
														v-model="scope.row.M5METDAT_HRRAIN"
													></el-input>
												</el-form-item>
											</template>
										</el-table-column>
										<el-table-column prop="M5METDAT_HRWNDV" label="风速（m/s）" min-width="120" align="center">
											<template slot-scope="scope">
												<el-form-item
													label-width="0px"
													:rules="rules.M5METDAT_HRWNDV"
													:prop="'M5METDATtab.'+scope.$index+'.M5METDAT_HRWNDV'"
												>
													<el-input
														oninput="value=value.replace(/^\D*(\d*(?:\.\d{0,20})?).*$/g, '$1')"
														class="bigForm"
														v-model="scope.row.M5METDAT_HRWNDV"
													></el-input>
												</el-form-item>
											</template>
										</el-table-column>
										<el-table-column prop="M5METDAT_IHRDIR" label="风向" min-width="120" align="center">
											<template slot-scope="scope">
												<el-form-item
													label-width="0px"
													:rules="rules.M5METDAT_IHRDIR"
													:prop="'M5METDATtab.'+scope.$index+'.M5METDAT_IHRDIR'"
												>
													<el-input
														oninput="value=value.replace(/^\D*(\d*(?:\.\d{0,20})?).*$/g, '$1')"
														class="bigForm"
														v-model="scope.row.M5METDAT_IHRDIR"
													></el-input>
												</el-form-item>
											</template>
										</el-table-column>
									</el-table>
								</div>
							</el-col>
						</el-row>
					</el-form>
				</div>
				<div class="bottom-btn">
					<el-button type="primary" :loading="loading" @click="submitFun">下一步</el-button>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
import selectPara from "@/components/selectPara"
import commCheckbox from "@/components/commCheckbox"
import ImageCropper from "@/components/ImageCropper"
import FixBottomButton from "@/components/FixBottomButton"
import ueditor from "@/components/ueditor"
import textLength from "@/components/common/comp/textLength"
import { commPara } from "@/mixins/mixins"
import { mapState } from "vuex"

import FileSaver from "file-saver"
import XLSX from "xlsx"

import { dome, sortverify, selecttext } from "@/utils/verify.js"

export default {
	name: "aboutUs",
	props: {
		batchid: String,
	},
	mixins: [commPara],
	components: {
		selectPara,
		ImageCropper,
		textLength,
		commCheckbox,
		FixBottomButton,
		ueditor,
	},

	data() {
		var M2LIMSPA001 = (rule, value, callback) => {
			if (value == "") {
				callback(new Error("输入值不能为空"))
			} else if (value < 1 || value > this.GENUMRAD001) {
				callback(new Error("输入范围为[1，" + this.GENUMRAD001 + "]"))
			} else {
				callback()
			}
		}
		var M4RNDSTS001 = (rule, value, callback) => { 
			let tempdata = value.trim().split(/\s+/) 
			if (parseFloat(tempdata[tempdata.length - 1]).toString() == "NaN") {
				callback(new Error("请输入正确的格式"))
			} else if (
				tempdata[tempdata.length - 1] < 0.001 ||
				tempdata[tempdata.length - 1] > 99.9
			) {
				callback(new Error("输入范围为[0.001，99.9]"))
			} else if (
				tempdata.length >= 1 &&
				tempdata[tempdata.length - 1] < tempdata[tempdata.length - 2]
			) {
				callback(
					new Error("多个距离输入请按照从小到大输入，以空隔分开")
				)
			} else if (tempdata.length !== this.ruleForm.M4NRNINT001) {
				callback(
					new Error("输入距离个数应为" + this.ruleForm.M4NRNINT001)
				)
			} else {
				callback()
			}
		}
		var M4RNRATE001 = (rule, value, callback) => { 
			let tempdata = value.trim().split(/\s+/) 
			if (parseFloat(tempdata[tempdata.length - 1]).toString() == "NaN") {
				callback(new Error("请输入正确的格式"))
			} else if (
				tempdata[tempdata.length - 1] < 0.001 ||
				tempdata[tempdata.length - 1] > 99.9
			) {
				callback(new Error("输入范围为[0.001，99.9]"))
			} else if (
				tempdata.length >= 1 &&
				tempdata[tempdata.length - 1] < tempdata[tempdata.length - 2]
			) {
				callback(
					new Error("多个距离输入请按照从小到大输入，以空隔分开")
				)
			} else if (tempdata.length !== this.ruleForm.M4NRINTN001) {
				callback(
					new Error("输入距离个数应为" + this.ruleForm.M4NRINTN001)
				)
			} else {
				callback()
			}
		}
		return {
			GENUMRAD001: 1,
			activeTab: "8",
			dialogVisible: false,
			ruleForm: {
				M1METCOD001: "1",
				M1METCOD001Name: "文件中固定开始时间",
				M2LIMSPA001: "",
				M2LIMSPA001Name: "",
				M2BNDMXH001: "1000",
				M2IBDSTB001: "4",
				M2BNDRAN001: "5",
				M2BNDWND001: "5",
				M3ISTRDY001: "0",
				M3ISTRHR001: "0",
				M4NRNINT001: "0",
				M4RNDSTS001: "0",
				M4NRINTN001: "0",
				M4RNRATE001: "0",
				M4NSMPLS001: "0",
				M4IRSEED001: "79",
				M4NSBINS001: "0",
				M4SMPLDFtab: [],
				M5METDATtab: [],
			},
			fileList: [],
			options: [],
			M1METCOD001options: [
				{
					label: "文件中固定开始时间",
					value: "1",
				},
				{
					label: "气象bin文件抽样",
					value: "2",
				},
				{
					label: "用户提供120h气象数据文件",
					value: "3",
				},
				{
					label: "边界层气象",
					value: "4",
				},
				{
					label: "等间距分层抽样",
					value: "5",
				},
			],
			M4NSMPLS001options: [],
			rules: {
				M2LIMSPA001: [
					{
						validator: selecttext,
						trigger: ["change", "blur"],
					},
				],
				M2BNDMXH001: [
					{
						validator: dome(1.0e2, 1.0e4),
						trigger: ["change", "blur"],
					},
				],

				M2IBDSTB001: [
					{
						validator: dome(1, 6),
						trigger: ["change", "blur"],
					},
				],
				M2BNDRAN001: [
					{
						validator: dome(0.0, 99.0),
						trigger: ["change", "blur"],
					},
				],
				M2BNDWND001: [
					{
						validator: dome(0.5, 30.0),
						trigger: ["change", "blur"],
					},
				],
				M3ISTRDY001: [
					{
						validator: dome(1, 365),
						trigger: ["change", "blur"],
					},
				],
				M3ISTRHR001: [
					{
						validator: dome(1, 24),
						trigger: ["change", "blur"],
					},
				],
				M4NRNINT001: [
					{
						validator: dome(4, 6),
						trigger: ["change", "blur"],
					},
				],
				M4RNDSTS001: [
					{
						validator: M4RNDSTS001,
						trigger: ["change", "blur"],
					},
				],
				M4NRINTN001: [
					{
						validator: dome(2, 3),
						trigger: ["change", "blur"],
					},
				],
				M4RNRATE001: [
					{
						validator: M4RNRATE001,
						trigger: ["change", "blur"],
					},
				],
				M4IRSEED001: [
					{
						validator: dome(0, 255),
						trigger: ["change", "blur"],
					},
				],
				M4SMPLDF_INDXBN: [
					{
						validator: dome(1, 1.0e30),
						trigger: ["change", "blur"],
					},
				],
				M4SMPLDF_INWGHT: [
					{
						validator: dome(1, 8760),
						trigger: ["change", "blur"],
					},
				],
				M5METDAT_IHRSTB: [
					{
						validator: dome(1, 6),
						trigger: ["change", "blur"],
					},
				],
				M5METDAT_HRRAIN: [
					{
						validator: dome(0, 99),
						trigger: ["change", "blur"],
					},
				],
				M5METDAT_HRWNDV: [
					{
						validator: dome(0, 30),
						trigger: ["change", "blur"],
					},
				],
				M5METDAT_HRMXHT: [
					{
						validator: dome(100, 10000),
						trigger: ["change", "blur"],
					},
				],
				M5METDAT_IHRDIR: [
					{
						validator: dome(1, 16),
						trigger: ["change", "blur"],
					},
				],
			},
			loading: false,
			WaterFileName:null
		}
	},
	computed: {
		...mapState({
			basic_Data: (state) => state.token.basic_Data,
		}),
		M2LIMSPA001options: function () {
			let tempGESPAEND_DISTANCE = this.basic_Data.GESPAEND_DISTANCE
				? this.basic_Data.GESPAEND_DISTANCE
				: []
			let temp = tempGESPAEND_DISTANCE.map((item, index) => {
				return {
					label: item,
					value: index + 1,
				}
			})
			return temp
		},
	},
	created() {},
	mounted() {
		this.getData()
	},
	methods: {
		getData() {
			//气象文件类型
			this.ruleForm.M1METCOD001 = this.basic_Data.M1METCOD001.toString()

			//气象参数设置
			this.ruleForm.M2LIMSPA001 = this.basic_Data.M2LIMSPA001.toString()
			this.ruleForm.M2BNDMXH001 = this.basic_Data.M2BNDMXH001.toString()
			this.ruleForm.M2IBDSTB001 = this.basic_Data.M2IBDSTB001.toString()
			this.ruleForm.M2BNDRAN001 = this.basic_Data.M2BNDRAN001.toString()
			this.ruleForm.M2BNDWND001 = this.basic_Data.M2BNDWND001.toString()
			this.ruleForm.M3ISTRDY001 = this.basic_Data.M3ISTRDY001.toString()
			this.ruleForm.M3ISTRHR001 = this.basic_Data.M3ISTRHR001.toString()
			this.ruleForm.M4NRNINT001 = this.basic_Data.M4NRNINT001
				? this.basic_Data.M4NRNINT001.toString()
				: ""
			if (
				this.ruleForm.M4NRNINT001 >= 4 ||
				this.ruleForm.M4NRNINT001 <= 6
			) {
				this.ruleForm.M4RNDSTS001 = this.basic_Data.M4RNDSTS001
					? this.basic_Data.M4RNDSTS001.join("\xa0")
					: ""
			}

			this.ruleForm.M4NRINTN001 = this.basic_Data.M4NRINTN001
				? this.basic_Data.M4NRINTN001.toString()
				: ""
			if (
				this.ruleForm.M4NRINTN001 >= 2 ||
				this.ruleForm.M4NRINTN001 <= 3
			) {
				this.ruleForm.M4RNRATE001 = this.basic_Data.M4RNRATE001
					? this.basic_Data.M4RNRATE001.join("\xa0")
					: ""
			}

			this.ruleForm.M4NSMPLS001 = this.basic_Data.M4NSMPLS001.toString()
			this.ruleForm.M4IRSEED001 = this.basic_Data.M4IRSEED001.toString()
			this.ruleForm.M4NSBINS001 = this.basic_Data.M4NSBINS001.toString()

			if (this.basic_Data.M4NSMPLS001 == 0) {
				for (
					let index = 0;
					index < parseInt(this.basic_Data.M4NSBINS001);
					index++
				) {
					this.ruleForm.M4SMPLDFtab.push({
						index: index + 1,
						M4SMPLDF_INDXBN: this.basic_Data.M4SMPLDF_INDXBN[index],
						M4SMPLDF_INWGHT: rhis.basic_Data.M4SMPLDF_INWGHT[index],
					})
				}
			}

			if (this.basic_Data.M4NSMPLS001 == 3) {
				this.ruleForm.M5METDATtab = []
				for (let index = 0; index < 120; index++) { 
					this.ruleForm.M5METDATtab.push({
						M5METDAT_HRMXHT: this.basic_Data.M5METDAT_HRMXHT[index],
						M5METDAT_IHRSTB: this.basic_Data.M5METDAT_IHRSTB[index],
						M5METDAT_HRRAIN: this.basic_Data.M5METDAT_HRRAIN[index],
						M5METDAT_HRWNDV: this.basic_Data.M5METDAT_HRWNDV[index],
						M5METDAT_IHRDIR: this.basic_Data.M5METDAT_IHRDIR[index],
					})
				}
			}
		},
		handleRemove(file, fileList) { 
		},
		handlePreview(file) { 
		},
		handleExceed(files, fileList) {
			this.$message.warning(
				`当前限制选择 3 个文件，本次选择了 ${
					files.length
				} 个文件，共选择了 ${files.length + fileList.length} 个文件`
			)
		},
		beforeRemove(file, fileList) {
			return this.$confirm(`确定移除 ${file.name}？`)
		},
		//上传气象文件
		uploadWeaterFile(file){
		},
		beforeUpload(file){
			var reader = new FileReader()
			reader.onload = (e)=>{
				var byts = new Uint8Array(reader.result)
				var bytarr = []
				var byta = []
				for (var i = 0; i < byts.length; i++) {
					if(i%10000==0){
						byta.push(byts[i]);
						bytarr.push(byta)
						byta=[]
					}else{
						byta.push(byts[i]);
					}
				}
				
				this.$appApi.TEST_GETFILES({filetype:'HD5QIXIANGFILES'}).then(res=>{
					if(res.data){
						let fileNames = res.data.substring(8);
						if(fileNames.indexOf(file.name)!=-1){
							this.$confirm('服务器上已有相同文件名的文件?', '提示', {
								confirmButtonText: '覆盖',
								cancelButtonText: '追加',
								type: 'warning'
								}).then(() => {
									let flieFlag = 'TRUE'
									bytarr.forEach((item,index)=>{
										if(index==0){
											flieFlag = 'TRUE'
										}else{
											flieFlag = 'FALSE'
										}
										this.$appApi.TEST_UPFILE({
											fileName:file.name,
											buffer:item,
											filetype:'HD5QIXIANGFILES',
											delfile:flieFlag
										}).then(result=>{
											// console.log(result)
											this.WaterFileName = file.name
										})
									})
									
								}).catch(() => {
									bytarr.forEach((item,index)=>{
										this.$appApi.TEST_UPFILE({
											fileName:file.name,
											buffer:item,
											filetype:'HD5QIXIANGFILES',
											delfile:'FALSE'
										}).then(result=>{
											this.WaterFileName = file.name
										})
									})
									
										
							});
						}else{
							bytarr.forEach((item,index)=>{
								this.$appApi.TEST_UPFILE({
									fileName:file.name,
									buffer:item,
									filetype:'HD5QIXIANGFILES',
									delfile:'FALSE'
								}).then(result=>{
									// console.log(result)
									this.WaterFileName = file.name
								})
							})
						}
					}
				})
			}
			reader.readAsArrayBuffer(file)
			// let reader = new FileReader();
			// console.log(12345)
			// FileReader.prototype.readAsBinaryString = function (file) {
			// 	console.log(88888)
			// 	var reader = new FileReader()
			// 	reader.onload = function (e) {
			// 		var bytes = new Uint8Array(reader.result)
			// 		console.log(bytes)
			// 		for (var i = 0; i < byts.length; i++) {
			// 			bytarr.push(byts[i]);
			// 			console.log(bytarr)
			// 		}
			// 		console.log(bytarr)
			// 	}
			// 	reader.readAsArrayBuffer(file)
			// }
			// reader.readAsBinaryString(file)
			
		},
		M1METCOD001Change() {
			this.M4NSMPLS001options = []
			if (this.ruleForm.M1METCOD001 == 2) {
				for (let index = 0; index < 25; index++) {
					this.M4NSMPLS001options.push({
						label: index.toString(),
						value: index.toString(),
					})
				}
			} else if (this.ruleForm.M1METCOD001 == 5) {
				let tempArray = [1, 2, 3, 4, 6, 8, 12, 24]
				this.M4NSMPLS001options = tempArray.map((item) => {
					// console.log(item)
					return {
						label: item,
						value: item,
					}
				})
			} else {
			}
		},
		M4NSMPLS001Change() {
			this.ruleForm.M4SMPLDFtab = []
			this.ruleForm.M5METDATtab = []
			if (this.ruleForm.M4NSMPLS001 == 0) {
				this.ruleForm.M4NSBINS001 =
					parseInt(this.ruleForm.M4NRNINT001) *
						(parseInt(this.ruleForm.M4NRINTN001) + 1) +
					16
				for (
					let index = 1;
					index <= parseInt(this.ruleForm.M4NSBINS001);
					index++
				) {
					this.ruleForm.M4SMPLDFtab.push({
						index: index,
						M4SMPLDF_INDXBN: this.basic_Data.M4SMPLDF_INDXBN[
							index - 1
						],
						M4SMPLDF_INWGHT: this.basic_Data.M4SMPLDF_INWGHT[
							index - 1
						],
					})
				}
			}
			if (this.ruleForm.M4NSMPLS001 == 3) {
				for (let index = 0; index < 120; index++) {
					this.ruleForm.M5METDATtab.push({
						M5METDAT_HRMXHT: this.basic_Data.M5METDAT_HRMXHT[index],
						M5METDAT_IHRSTB: this.basic_Data.M5METDAT_IHRSTB[index],
						M5METDAT_HRRAIN: this.basic_Data.M5METDAT_HRRAIN[index],
						M5METDAT_HRWNDV: this.basic_Data.M5METDAT_HRWNDV[index],
						M5METDAT_IHRDIR: this.basic_Data.M5METDAT_IHRDIR[index],
					})
				}
			}
		},
		exportExcel() {
			var that = this
			var arry = [["气象类别编号", "气象bin编号", "气象序列数量"]]
			// 要导出的历史数据
			that.ruleForm.M4SMPLDFtab.map((a) => {
				var _arry = []
				_arry.push(a.index.toString())
				_arry.push(a.M4SMPLDF_INDXBN.toString())
				_arry.push(a.M4SMPLDF_INWGHT.toString())
				return _arry
			}).forEach((a) => {
				arry.push(a)
			})
			var sheetHistory = XLSX.utils.aoa_to_sheet(arry)
			var workbook = XLSX.utils.book_new()
			XLSX.utils.book_append_sheet(workbook, sheetHistory, "气象类别数目")
			sheetHistory["!cols"] = [{ wpx: 150 }, { wpx: 90 }, { wpx: 150 }] //单元格列宽

			try {
				var wbOut = XLSX.write(workbook, {
					bookType: "xlsx",
					bookSST: true,
					type: "array",
				})
				FileSaver.saveAs(
					new Blob([wbOut], { type: "application/octet-stream" }),
					"气象类别数目.xlsx"
				)
			} catch (e) {
				if (typeof console !== "undefined") console.log(e, wbOut)
			}
		},
		daochu() {
			var that = this
			var arry = [
				["混合层高度", "稳定度类型", "降雨速度", "风速", "风向"],
			]
			// 要导出的历史数据
			that.ruleForm.M5METDATtab.map((a) => {
				var _arry = []
				_arry.push(a.M5METDAT_HRMXHT.toString())
				_arry.push(a.M5METDAT_IHRSTB.toString())
				_arry.push(a.M5METDAT_HRRAIN.toString())
				_arry.push(a.M5METDAT_HRWNDV.toString())
				_arry.push(a.M5METDAT_IHRDIR.toString())
				return _arry
			}).forEach((a) => {
				arry.push(a)
			})
			var sheetHistory = XLSX.utils.aoa_to_sheet(arry)
			var workbook = XLSX.utils.book_new()
			XLSX.utils.book_append_sheet(workbook, sheetHistory, "气象参数")
			sheetHistory["!cols"] = [{ wpx: 150 }, { wpx: 90 }, { wpx: 150 }] //单元格列宽

			try {
				var wbOut = XLSX.write(workbook, {
					bookType: "xlsx",
					bookSST: true,
					type: "array",
				})
				FileSaver.saveAs(
					new Blob([wbOut], { type: "application/octet-stream" }),
					"气象参数.xlsx"
				)
			} catch (e) {
				if (typeof console !== "undefined") console.log(e, wbOut)
			}
		},
		importf(obj) {
			//导入数据
			let _this = this
			let inputDOM = this.$refs.inputer // 通过DOM取文件数据
			this.file = event.currentTarget.files[0]
			var rABS = false //是否将文件读取为二进制字符串
			var f = this.file
			var reader = new FileReader()
			//if (!FileReader.prototype.readAsBinaryString) {
			FileReader.prototype.readAsBinaryString = function (f) {
				var binary = ""
				var rABS = false //是否将文件读取为二进制字符串
				var pt = this
				var wb //读取完成的数据
				var outdata
				var reader = new FileReader()
				reader.onload = function (e) {
					var bytes = new Uint8Array(reader.result)
					var length = bytes.byteLength
					for (var i = 0; i < length; i++) {
						binary += String.fromCharCode(bytes[i])
					}
					var XLSX = require("xlsx")
					if (rABS) {
						wb = XLSX.read(btoa(fixdata(binary)), {
							//手动转化
							type: "base64",
						})
					} else {
						wb = XLSX.read(binary, {
							type: "binary",
						})
					}
					// outdata就是你想要的东西 excel导入的数据
					outdata = XLSX.utils.sheet_to_json(
						wb.Sheets[wb.SheetNames[0]]
					)
					// excel 数据再处理
					let arr = []
					if (_this.daorutype == 1) {
						outdata.map((v) => {
							let obj = {}
							obj.index = v.气象类别编号
							obj.M4SMPLDF_INDXBN = v.气象bin编号
							obj.M4SMPLDF_INWGHT = v.气象序列数量
							arr.push(obj)
						})
						_this.ruleForm.M4SMPLDFtab = [...arr]
					} else if (_this.daorutype == 2) {
						outdata.map((v) => {
							let obj = {}
							obj.M5METDAT_HRMXHT = v.混合层高度
							obj.M5METDAT_IHRSTB = v.稳定度类型
							obj.M5METDAT_HRRAIN = v.降雨速度
							obj.M5METDAT_HRWNDV = v.风速
							obj.M5METDAT_IHRDIR = v.风向
							arr.push(obj)
						})
						_this.ruleForm.M5METDATtab = [...arr]
					}
				}
				reader.readAsArrayBuffer(f)
			}
			if (rABS) {
				reader.readAsArrayBuffer(f)
			} else {
				reader.readAsBinaryString(f)
			}
		},
		daoru(val) {
			this.daorutype = val
			if (this.daorutype == 1) {
				this.$refs.file.click()
			} else if (this.daorutype == 2) {
				this.$refs.file2.click()
			}
		},
		submitFun() {
			this.$refs.form.validate((valid) => {
				if (valid) {
					let tempM4RNDSTS001 = this.ruleForm.M4RNDSTS001.trim()
						.split(/\s+/)
						.map((item) => {
							return parseFloat(item)
						})
					let tempM4RNRATE001 = this.ruleForm.M4RNRATE001.trim()
						.split(/\s+/)
						.map((item) => {
							return parseFloat(item)
						})
					let template = JSON.parse(JSON.stringify(this.basic_Data))
					template.M1METCOD001 = parseInt(this.ruleForm.M1METCOD001)
					template.M2LIMSPA001 = parseInt(this.ruleForm.M2LIMSPA001)
					template.M2BNDMXH001 = parseFloat(this.ruleForm.M2BNDMXH001)
					template.M2IBDSTB001 = parseInt(this.ruleForm.M2IBDSTB001)
					template.M2BNDRAN001 = parseFloat(this.ruleForm.M2BNDRAN001)
					template.M2BNDWND001 = parseFloat(this.ruleForm.M2BNDWND001)
					
					if (
						template.M1METCOD001 == 1 ||
						template.M1METCOD001 == 3 ||
						template.M1METCOD001 == 5
					) {
						template.M3ISTRDY001 = parseInt(
							this.ruleForm.M3ISTRDY001
						)
						template.M3ISTRHR001 = parseInt(
							this.ruleForm.M3ISTRDY001
						)
					} else {
						template.M3ISTRDY001 = 0
						template.M3ISTRHR001 = 0
					}

					if (template.M1METCOD001 == 2) {
						template.M4NRNINT001 = parseInt(
							this.ruleForm.M4NRNINT001
						)
						template.M4RNDSTS001 = tempM4RNDSTS001
						template.M4NRINTN001 = parseInt(
							this.ruleForm.M4NRINTN001
						)

						template.M4RNRATE001 = tempM4RNRATE001
					} else {
						template.M4NRNINT001 = 0
						template.M4RNDSTS001 = []
						template.M4NRINTN001 = 0
						template.M4RNRATE001 = []
					}

					if (
						template.M1METCOD001 == 2 ||
						template.M1METCOD001 == 5
					) {
						template.M4NSMPLS001 = parseInt(
							this.ruleForm.M4NSMPLS001
						)
					} else {
						template.M4NSMPLS001 = 0
					}

					template.M4IRSEED001 = parseInt(this.ruleForm.M4IRSEED001)

					template.M4NSBINS001 = parseInt(this.ruleForm.M4NSBINS001)

					template.M4SMPLDF_INDXBN = this.ruleForm.M4SMPLDFtab.map(
						(item) => {
							return parseInt(item.M4SMPLDF_INDXBN)
						}
					)
					template.M4SMPLDF_INWGHT = this.ruleForm.M4SMPLDFtab.map(
						(item) => {
							return parseInt(item.M4SMPLDF_INWGHT)
						}
					)
					if (template.M4NSMPLS001 == "3") {
						template.M5METDAT_HRMXHT = this.ruleForm.M5METDATtab.map(
							(item) => {
								return parseFloat(item.M5METDAT_HRMXHT)
							}
						)
						template.M5METDAT_IHRSTB = this.ruleForm.M5METDATtab.map(
							(item) => {
								return parseInt(item.M5METDAT_IHRSTB)
							}
						)
						template.M5METDAT_HRRAIN = this.ruleForm.M5METDATtab.map(
							(item) => {
								return parseFloat(item.M5METDAT_HRRAIN)
							}
						)
						template.M5METDAT_HRWNDV = this.ruleForm.M5METDATtab.map(
							(item) => {
								return parseFloat(item.M5METDAT_HRWNDV)
							}
						)
						template.M5METDAT_IHRDIR = this.ruleForm.M5METDATtab.map(
							(item) => {
								return parseFloat(item.M5METDAT_IHRDIR)
							}
						)
					} else {
						template.M5METDAT_HRMXHT = []
						template.M5METDAT_IHRSTB = []
						template.M5METDAT_HRRAIN = []
						template.M5METDAT_HRWNDV = []
						template.M5METDAT_IHRDIR = []
					}
					template.QIXIANGNAME = this.WaterFileName
					template.SETPAGEINDEX = this.activeTab
					this.$store.dispatch("upData_basic_Data", template)
					let params = {
						batchid: this.$props.batchid,
						batchinfo: "BASE",
						submitjson: JSON.stringify(template),
					}
					this.$appApi
						.BATCH_SUBMIT(params)
						.then((res) => {
							var str = res.data.substring(0, 1)
							if (str == "E") {
								let message = res.data.substring(6)
								this.$notify.error({
									title: "保存失败！",
									message: message,
									type: "success",
								})
							} else if (str == "S") {
								this.$notify({
									message: "保存成功！",
									type: "success",
								})
								this.$emit("changeActiveTab", this.activeTab)
					            this.$store.dispatch("change_basic_showTab", this.activeTab)
								if(activeTab >=9){
									this.$router.push({
										path: "/home",
									})
								}
							}
						})
						.catch((err) => {
							console.log(err)
						})
				}
			})
		},
	},
}
</script>
<style lang="scss" scoped>
.page-flex-row /deep/ .el-collapse-item__header {
	background: #f3f9fe;
}
.operation {
	width: 95%;
	height: 80px;
	display: flex;
	justify-content: flex-end;
	align-items: center;
}
#img-upload {
	display: none;
}
</style>